//
//  MainViewController.swift
//  微博模拟项目
//
//  Created by 尹家鑫 on 16/3/12.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//

import UIKit


class MainViewController: UITabBarController {
    
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let mainTabbar = MainTabBar()
        
        mainTabbar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        //KVC
        print(tabBar.classForCoder)
        setValue(mainTabbar, forKey: "tabBar")
        print(tabBar.classForCoder)
        
        //添加所有的子视图控制器
        addChildViewControllers()
        
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK:添加子视图控制器
    private func addChildViewController(vc: UIViewController,title: String, imageName: String) {
        //设置title
        vc.title = title
        vc.tabBarItem.title = title
        
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
}
